<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="类别" prop="type1">
        <el-input
          v-model="queryParams.type1"
          placeholder="请输入类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="产品" prop="gg">
        <el-input
          v-model="queryParams.gg"
          placeholder="请输入产品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="ggname">
        <el-input
          v-model="queryParams.ggname"
          placeholder="请输入产品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="所属工厂" prop="factoryId" v-show="false">
        <!-- <el-input
          v-model="queryParams.factoryId"
          placeholder="请输入所属工厂"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.factoryId" placeholder="请选择所属工厂" clearable size="small">
          <el-option v-for="dict in factoryListOptions" :key="dict.deptId" :label="dict.name" :value="dict.deptId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="rq">
        <el-date-picker :editable="false" clearable size="small" v-model="queryParams.rq" type="month" value-format="yyyy-MM" placeholder="选择统计时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['unstand:unstand:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['unstand:unstand:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['unstand:unstand:remove']">删除</el-button>
      </el-col>
      <!--   <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['unstand:unstand:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="unstandList" @selection-change="handleSelectionChange" max-height="500">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="rq" width="180" />
      <el-table-column label="类别" align="center" prop="type1" />
      <!-- <el-table-column label="产品" align="ceWnter" prop="gg" /> -->
      <!--    <el-table-column label="日期" align="center" prop="rq" width="180" />-->
      <el-table-column label="产品" align="center" prop="ggname" />
      <el-table-column label="内控" align="center" prop="internalControl" />
      <el-table-column label="实际" align="center" prop="actualValue" />
      <!--      <el-table-column label="原因" align="center" prop="season" />
      <el-table-column label="措施" align="center" prop="measures" />
      <el-table-column label="所属工厂" align="center" prop="factoryName" />-->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="excelhandleView(scope.row)">导出</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['unstand:unstand:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['unstand:unstand:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改不达标项对话框 -->
    <el-dialog fullscreen :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别" prop="type1">
              <!-- <el-input
                v-model="form.type1"
                placeholder="请输入类别(原物料、水、电)"
                :disabled="openDisabled"
              /> -->
              <el-select v-model="form.type1" placeholder="请选择类别" :disabled="openDisabled" @change="type1change">
                <el-option value="原物料消耗率">原物料消耗率</el-option>
                <el-option value="水利用率">水利用率</el-option>
                <el-option value="吨耗电">吨耗电</el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="产品" prop="ggname">
              <el-select v-model="form.ggname" placeholder="请选择" @change="ggchangefun($event)">
                <el-option-group v-for="group in options" :key="group.label" :label="group.label">
                  <el-option v-for="item in group.options" :key="item.dictValue" :label="item.dictLabel" :value="{ value:item.dictValue,label:item.dictLabel  }">
                  </el-option>
                </el-option-group>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品" prop="gg" v-show="false">
              <el-input v-model="form.gg" placeholder="请输入产品" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属工厂" prop="factoryId">
              <!-- <el-input
                v-model="form.factoryId"
                placeholder="请输入所属工厂"
                :disabled="openDisabled"
              /> -->
              <el-select v-model="form.factoryId" placeholder="请选择所属工厂" clearable size="small" :disabled="true">
                <el-option v-for="dict in factoryListOptions" :key="dict.deptId" :label="dict.name" :value="dict.deptId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日期" prop="rq">
              <el-date-picker :editable="false" clearable size="small" v-model="form.rq" type="month" value-format="yyyy-MM" placeholder="选择统计时间" :disabled="openDisabled" @change="datechange">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="内控指标值" prop="internalControl">
              <el-input type="number" v-model="form.internalControl" placeholder="请输入内控指标" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际指标值" prop="actualValue">
              <el-input type="number" v-model="form.actualValue" placeholder="请输入实际指标" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="原因" prop="season">
              <el-input v-model="form.season" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="措施" prop="measures">
              <el-input v-model="form.measures" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUnstand,
  getUnstand,
  delUnstand,
  addUnstand,
  updateUnstand,
  getinternalControl,
} from "@/api/report/complain/unstand";
import { getFactoryList } from "@/api/report/complain/complain";
export default {
  name: "Unstand",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 不达标项表格数据
      unstandList: [],
      // 所属工厂
      factoryListOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 产品分类
      product_report_type: [],
      options: [],
      Productionline_Hygiene: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        type1: null,
        gg: null,
        rq: null,
        ggname: null,
        season: null,
        measures: null,
        factoryId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryId: [{ required: true, message: "所属工厂不能为空" }],
        type1: [{ required: true, message: "类别不能为空" }],
        ggname: [{ required: true, message: "产品不能为空" }],
        rq: [{ required: true, message: "日期不能为空" }],
      },
    };
  },
  created() {
    this.queryParams.factoryId = 101;
    this.getList();
    this.getFactoryList();
    this.getDicts("product_report_type").then((response) => {
      this.product_report_type = response.data;
    });
    this.getDicts("seven_material_ggname").then((response) => {
      this.Productionline_Hygiene = response.data;
    });
  },
  methods: {
    datechange(value) {
      var queryParamT = {
        type1: this.form.type1,
        gg: this.form.gg,
        ggname: this.form.ggname,
        rq: this.form.rq,
      };
      getinternalControl(queryParamT).then((response) => {
        this.form.internalControl = response.data.internalControl;
        this.form.actualValue = response.data.actualValue;
      });
    },
    dialogChange(event) {
      this.form.gg = event.options.code;
      this.form.ggname = event.label;
    },
    //工厂信息
    getFactoryList() {
      getFactoryList().then((response) => {
        this.factoryListOptions = response.data;
      });
    },
    /** 查询不达标项列表 */
    getList() {
      this.loading = true;
      listUnstand(this.queryParams).then((response) => {
        this.unstandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type1: null,
        gg: null,
        ggname: null,
        rq: null,
        season: null,
        measures: null,
        factoryId: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        internalControl: null,
        actualValue: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加不达标项";
      this.form.factoryId = 101;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getUnstand(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改不达标项";
        if (this.form.type1 == "原物料消耗率") {
          this.options = [
            {
              options: this.Productionline_Hygiene,
            },
          ];
        } else {
          this.options = [
            {
              options: this.product_report_type,
            },
          ];
        }
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getUnstand(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看不达标项";
      });
    },
    //类型变化
    ggchangefun(val) {
      const { value, label } = val;
      this.form.gg = value;
      this.form.ggname = label;
    },
    /** 类别变化 */
    type1change(selvalue) {
      this.form.gg = "";
      this.form.ggname = "";
      if (selvalue == "原物料消耗率") {
        this.options = [
          {
            options: this.Productionline_Hygiene,
          },
        ];
      } else {
        this.options = [
          {
            options: this.product_report_type,
          },
        ];
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUnstand(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addUnstand(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除不达标项编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUnstand(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    excelhandleView(row) {
      this.download(
        "productionProcess/unstand/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "七率不达标项" + this.parseTime(new Date()) + ".xlsx"
      );
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "unstand/unstand/export",
        {
          ...this.queryParams,
        },
        "不达标项" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>

